package com.ch.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;

/**
 * @program: 123start
 * @description:
 * @author: chenhang
 * @create: 2023-01-10 14:24
 */

//继承 CommonServlet
//        =>HttpServlet(专用http协议)
//            =>GenericServlet(应用于所有协议 http,ftp...)
//                =>Servlet,ServletConfig(配置接口)
//实现
//        实现doXXX方法
//    实现doXXX 方法： doGet 和 doPost 是最常用的
//    doGet只能处理 get 类型的请求
//    doPost只能处理 post 类型的请求
//配置
//        1. web.xml
//        2. 注解方式
//                配置地址：1.  /开头,不以 / 会到服务器启动失败
//                        2.  地址全局唯一
//                404:地址错误
//                405:方法不正确
//                    post请求 ==> form提交 or AJAX提交
//                    其他都是  get  请求

@WebServlet(value = "/week" , loadOnStartup = 2 , initParams = @WebInitParam(name="myname",value="李四"))
public class CommonServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Calendar calendar = Calendar.getInstance();
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        resp.getWriter().print("星期："+week);
    }

    @Override
    public void destroy() {
        System.out.println("销毁啦");
    }

    @Override
    public void init() throws ServletException {
        ServletConfig servletConfig = this.getServletConfig();
        String myname = servletConfig.getInitParameter("myname");
        System.out.println("初始化" +myname);
    }
}
